var errorLogger = require("pomelo-logger").getLogger("error-log");
var util = require("../../../../util/util.js")
//比赛场匹配
module.exports = function(manager) {
	this.manager = manager
	this.users = {} 						  //全部玩家列表
	this.curUsers = [] 						  //当前晋级玩家列表
	this.cid = "" 							  //比赛类型
	this.numeration = ""					  //比赛编号
	this.maxRound = 0                         //最大轮数
	this.curRound = 0 						  //当前轮数
	this.curWin = 0 						  //本轮赢家数
	this.maxWin = 0 						  //本轮最大赢家
	this.singlePlayerNum = 0  				  //单局游戏人数
	this.curCounterpartTable = []             //当轮对战表  {0 : user1,1 : user2,2 : user3}
	this.allCounterpartTable = [] 			  //全部对战表  [{0 : user1,1 : user2,2 : user3,win : false}]
	this.robotTimerInfo = {}				  //机器人计时器
	this.beginFlag = false 					  //开始标识
	//比赛初始化
	this.init = function(cid,numeration,users,singlePlayerNum,gameType) {
		this.users = users
		this.cid = cid
		this.numeration = numeration
		this.singlePlayerNum = singlePlayerNum
		this.maxRound = Math.round(Math.log(this.users.length)/Math.log(this.singlePlayerNum))
		this.gameType = gameType
		for(var i in this.users){
			this.curUsers.push(this.users[i])
		}
		setTimeout(this.nextRound.bind(this),3000) 
	}
	//主循环
	this.mainTimer = function(curTime) {
		for(var i in this.robotTimerInfo){
			if(curTime > this.robotTimerInfo[i].time){
				this.competitionRoomFinish(this.robotTimerInfo[i].RoomIndex,this.robotTimerInfo[i].winUid,this.robotTimerInfo[i].score)
				delete this.robotTimerInfo[i]
			}
		}
	}
	//开始分配下一轮比赛
	this.nextRound = function() {
		this.beginFlag = true
		console.log("nextRound : ")
		this.curRound++
		this.maxWin = Math.ceil(this.curUsers.length / this.singlePlayerNum)
		this.curWin = 0
		this.curCounterpartTable = []
		//随机排序
		this.curUsers.sort(function(a,b){return Math.random() > 0.5 ? -1 : 1;});
		//组成对局
		for(var i = 0;i < this.maxWin;i++){
			var info = []
			for(var j = 0;j < this.singlePlayerNum;j++){
				var userInfo = util.deepCopy(this.curUsers.pop())
				delete userInfo.isWin
				info.push(userInfo)
			}
			this.curCounterpartTable.push(info)
		}
		console.log("this.curCounterpartTable",this.curCounterpartTable)
		var allFlag = false
		//开始分配房间
		for(var i = 0;i < this.curCounterpartTable.length;i++){
			var RoomIndex = i
			let users = this.curCounterpartTable[i]
			var flag = false
			for(var j in users){
				if(users[j].uid > 10000){
					flag = true
					allFlag = true
					break
				}
			}
			if(flag){
				console.log("玩家房间")
				manager.groupNewCompetitionRoom(this.numeration,RoomIndex,this.curRound,this.maxRound,users,this.gameType,this.cid)
			}else{
				//机器人场设定一定时间后结束
				var winUid = users[Math.floor(Math.random() * users.length)].uid
				var score = Math.floor(Math.random() * 20) + 5
				var time = Math.floor(Math.random() * 30000) + 70000
				this.robotTimerInfo[RoomIndex] = {
					RoomIndex : RoomIndex,
					winUid : winUid,
					score : score,
					time : time + (new Date()).getTime()
				}
				console.log("机器人房间",time)
			}
		}
		if(!allFlag){
			//只剩机器人，比赛场结束
			this.competitionFinish(0,{})
		}
		this.curUsers = []
	}
	//小局比赛结束
	this.competitionRoomFinish = function(RoomIndex,winUid,score) {
		console.log("competitionRoomFinish : ",RoomIndex,winUid)
		if(this.curCounterpartTable[RoomIndex]){
			var flag = false
			var winIndex = false
			for(var i in this.curCounterpartTable[RoomIndex]){
				if(this.curCounterpartTable[RoomIndex][i].uid == winUid && !this.curCounterpartTable[RoomIndex][i].isWin){
					flag = true
					winIndex = i
					break
				}
			}
			if(flag){
				for(var i in this.curCounterpartTable[RoomIndex]){
					if(this.curCounterpartTable[RoomIndex][i].uid != winUid){
						//解锁玩家
						this.manager.deblocking(this.curCounterpartTable[RoomIndex][i].uid)
					}
				}
				this.curCounterpartTable[RoomIndex][winIndex].isWin = true
				this.curUsers.push(this.curCounterpartTable[RoomIndex][winIndex])
				this.curWin++
				if(this.curWin >= this.maxWin){
					this.roundFinish(winUid,this.curCounterpartTable[RoomIndex])
				}
			}else{
				new Error("winUid not find : "+winUid+"  : "+JSON.stringify(this.curCounterpartTable[RoomIndex]))
				errorLogger.info("winUid not find : "+winUid+"  : "+JSON.stringify(this.curCounterpartTable[RoomIndex]))
			}
		}else{
			new Error("RoomIndex not find : "+this.numeration+"  : "+RoomIndex)
			errorLogger.info("RoomIndex not find : "+this.numeration+"  : "+RoomIndex)
		}
	}
	//本轮比赛结束
	this.roundFinish = function(winUid,players) {
		console.log("roundFinish : "+winUid)
		for(var i = 0;i < this.curCounterpartTable.length;i++){
			for(var j = 0;j < this.curCounterpartTable[i].length;j++){
				this.curCounterpartTable[i][j] = {
					uid : this.curCounterpartTable[i][j].uid,
					nickname : this.curCounterpartTable[i][j].nickname,
					head : this.curCounterpartTable[i][j].head,
					isWin : this.curCounterpartTable[i][j].isWin || false
				}
			}
		}
		this.allCounterpartTable.push(this.curCounterpartTable)
		if(this.curRound >= this.maxRound){
			this.competitionFinish(winUid,players)
		}else{
			setTimeout(this.nextRound.bind(this),8000)
		}
	}
	//决赛结束,比赛完成
	this.competitionFinish = function(winUid,players) {
		console.log("competitionFinish : "+winUid)
		this.manager.deblocking(winUid)
		//比赛结束
		this.manager.competitionFinish(this.numeration,winUid,players)
	}
	//获取比赛场信息
	this.getCompetitionInfo =function(cb) {
		if(this.beginFlag === false){
			cb(true,{"waitFlag" : true,"cid" : this.cid,"userList" : this.curUsers})
		}else{
			var info  = {}
			info.numeration = this.numeration
			info.maxRound = this.maxRound
			info.curRound = this.curRound
			info.curWin = this.curWin
			info.maxWin = this.maxWin
			info.cid = this.cid
			info.count = this.curCounterpartTable.length
			if(this.maxWin <= 3){
				info.curCounterpartTable = this.curCounterpartTable
			}
			cb(true,info)
		}
	}
}